package com.sduept.nwld.dataserver.manager.gismap;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class MapGisPersistentService {
//	@Resource(name="mapDataSource")
	private DataSource server_datasource;
//	@Resource
//	private UserTransaction ut;
	//变电站、杆塔表、 单、回、直流、无杆塔线路SQL for JDBC构造
	private static final String INERT_STATION = "INSERT INTO "+SuperMapProperties.STATION_TABLE+" (`SmX`, `SmY`,`id`,`name`,`subControlArea`,`substationType`,`altitude`,`priority`,`pasId`,`voltageLevel`,`voltage`,`flag`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
	private static final String UPDATE_STATION = "UPDATE "+SuperMapProperties.STATION_TABLE+" set `SmX`=?, `SmY`=? ,`name`=?,`subControlArea`=?,`substationType`=?,`altitude`=?,`priority`=?,`pasId`=?,`voltageLevel`=?,`voltage`=?,`flag`=? where id=?;";
	private static final String DELETE_STATION="DELETE FROM "+SuperMapProperties.STATION_TABLE+" where id=?";
	private static final String INERT_TOWER = "INSERT INTO "+SuperMapProperties.TOWER_TABLE+" (`SmX`, `SmY`,`id`,`name`,`line_cim_id`,`linename`,`voltage`,`towerserialno`,`isshow`,`sel`,`towerno`,`is_loop`,`loopname`,`loop_cim_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);";
	private static final String UPDATE_TOWER = "UPDATE "+SuperMapProperties.TOWER_TABLE+" set `SmX`=?, `SmY`=? ,`name`=?,`line_cim_id`=?,`linename`=?,`voltage`=?,`towerserialno`=?,`isshow`=?,`sel`=?,`towerno`=?,`is_loop`=?,`loopname`=?,`loop_cim_id`=? where id=?;";
	private static final String DELETE_TOWER="DELETE FROM "+SuperMapProperties.TOWER_TABLE+" where id=?";
	private static final String DELETE_TOWER_BY_LINE="DELETE FROM "+SuperMapProperties.TOWER_TABLE+" where line_cim_id=?";
	private static final String SEARCH_SINGLE_LINE="select lineId,lineName from "+SuperMapProperties.SINGLE_LINE_TABLE+" where lineId=?;";
	private static final String INERT_SINGLE_LINE = "INSERT INTO "+SuperMapProperties.SINGLE_LINE_TABLE+" (`smSdriE`, `smSdriN`,`smSdriS`, `smSdriW`,`SmGeometry`,`lineId`,`lineName`) VALUES (?,?,?,?,?,?,?);";
	private static final String UPDATE_SINGLE_LINE = "UPDATE "+SuperMapProperties.SINGLE_LINE_TABLE+" set `smSdriE`=?, `smSdriN`=?,`smSdriS`=?, `smSdriW`=?,`SmGeometry`=?, `lineName`=? where lineId=?;";
	private static final String DELETE_SINGLE_LINE="DELETE FROM "+SuperMapProperties.SINGLE_LINE_TABLE+" where lineId=?;";
	private static final String SEARCH_LOOP_LINE="select lineId,lineName from "+SuperMapProperties.LOOP_LINE_TABLE+" where lineId=?;";
	private static final String INERT_LOOP_LINE = "INSERT INTO "+SuperMapProperties.LOOP_LINE_TABLE+" (`smSdriE`, `smSdriN`,`smSdriS`, `smSdriW`,`SmGeometry`,`lineId`,`lineName`,`loopId`,`loopName`) VALUES (?,?,?,?,?,?,?,?,?);";
	private static final String UPDATE_LOOP_LINE = "UPDATE "+SuperMapProperties.LOOP_LINE_TABLE+" set `smSdriE`=?, `smSdriN`=?,`smSdriS`=?, `smSdriW`=?,`SmGeometry`=?, `lineName`=? where lineId=?;";
	private static final String DELETE_LOOP_LINE="DELETE FROM "+SuperMapProperties.LOOP_LINE_TABLE+" where lineId=?;";
	private static final String SEARCH_DC_LINE="select lineId,lineName from "+SuperMapProperties.DC_LINE_TABLE+" where lineId=?;";
	private static final String INERT_DC_LINE = "INSERT INTO "+SuperMapProperties.DC_LINE_TABLE+" (`smSdriE`, `smSdriN`,`smSdriS`, `smSdriW`,`SmGeometry`,`lineId`,`lineName`) VALUES (?,?,?,?,?,?,?);";
	private static final String UPDATE_DC_LINE = "UPDATE "+SuperMapProperties.DC_LINE_TABLE+" set `smSdriE`=?, `smSdriN`=?,`smSdriS`=?, `smSdriW`=?,`SmGeometry`=?, `lineName`=? where lineId=?;";
	private static final String DELETE_DC_LINE="DELETE FROM "+SuperMapProperties.DC_LINE_TABLE+" where lineId=?;";
	private static final String SEARCH_NOTOWER_LINE="select lineId,lineName from "+SuperMapProperties.NOTOWER_LINE_TABLE+" where lineId=?;";
	private static final String INERT_NOTOWER_LINE = "INSERT INTO "+SuperMapProperties.NOTOWER_LINE_TABLE+" (`smSdriE`, `smSdriN`,`smSdriS`, `smSdriW`,`SmGeometry`,`lineId`,`lineName`) VALUES (?,?,?,?,?,?,?);";
	private static final String UPDATE_NOTOWER_LINE = "UPDATE "+SuperMapProperties.NOTOWER_LINE_TABLE+" set `smSdriE`=?, `smSdriN`=?,`smSdriS`=?, `smSdriW`=?,`SmGeometry`=?, `lineName`=? where lineId=?;";
	private static final String DELETE_NOTOWER_LINE="DELETE FROM "+SuperMapProperties.NOTOWER_LINE_TABLE+" where lineId=?;";
	
	public boolean insertSingleLine(MapGisUtil.GeoLine geoline) throws SQLException, IOException {
		boolean flag=true;		
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
//				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(INERT_SINGLE_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineId);
				pst.setString(7, geoline.lineName);
				pst.execute();
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	
	public boolean updateSingleLine(MapGisUtil.GeoLine geoline) throws SQLException, IOException{
		boolean flag=true;
		int result=0;
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(UPDATE_SINGLE_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineName);
				pst.setString(7, geoline.lineId);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	
	/**
	 * 删除一条线路的时候，该线路的杆塔也会删除
	 * @param lineId
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public boolean delSingleLine(String lineId) throws Exception {
		return delete(DELETE_SINGLE_LINE, lineId) & delete(DELETE_TOWER_BY_LINE, lineId);
	}
	
	
	/**
	 * 删除该线路的杆塔
	 * @param lineId
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public boolean delLineTowers(String lineId) throws Exception {
		return delete(DELETE_TOWER_BY_LINE, lineId);
	}
		
	public boolean insertLoopLine(MapGisUtil.GeoLine geoline)  throws SQLException, IOException{
		boolean flag=true;
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(INERT_LOOP_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineId);
				pst.setString(7, geoline.lineName);
				pst.setString(8, geoline.loopId);
				pst.setString(9, geoline.loopName);
				pst.execute();
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean updateLoopLine(MapGisUtil.GeoLine geoline)  throws SQLException, IOException{
		boolean flag=true;
		int result=0;
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(UPDATE_LOOP_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineName);
				pst.setString(7, geoline.lineId);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean delLoopLine(String lineId)  throws SQLException, IOException{
		boolean flag=true;
		int result=0;
		if (lineId!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(DELETE_LOOP_LINE);
				pst.setString(1, lineId);				
				pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean insertDcLine(MapGisUtil.GeoLine geoline)  throws SQLException, IOException{
		boolean flag=true;
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(INERT_DC_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineId);
				pst.setString(7, geoline.lineName);
				pst.execute();
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean updateDcLine(MapGisUtil.GeoLine geoline)  throws SQLException, IOException{
		boolean flag=true;
		int result=0;
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(UPDATE_DC_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineName);
				pst.setString(7, geoline.lineId);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean delDcLine(String lineId)  throws SQLException, IOException{
		boolean flag=true;
		int result=0;
		if (lineId!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(DELETE_DC_LINE);
				pst.setString(1, lineId);				
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean insertNoTowerLine(MapGisUtil.GeoLine geoline)  throws SQLException, IOException{
		boolean flag=true;
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(INERT_NOTOWER_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineId);
				pst.setString(7, geoline.lineName);
				pst.execute();
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean updateNoTowerLine(MapGisUtil.GeoLine geoline) throws SQLException, IOException{
		boolean flag=true;
		int result=0;
		if (geoline!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(UPDATE_NOTOWER_LINE);
				pst.setDouble(1, geoline.smSdriE);
				pst.setDouble(2, geoline.smSdriN);
				pst.setDouble(3, geoline.smSdriS);
				pst.setDouble(4, geoline.smSdriW);
				pst.setBytes(5, geoline.smGeometry);
				pst.setString(6, geoline.lineName);
				pst.setString(7, geoline.lineId);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean delNoTowerLine(String lineId) throws SQLException, IOException {
		boolean flag=true;
		int result=0;
		if (lineId!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(DELETE_NOTOWER_LINE);
				pst.setString(1, lineId);				
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	
	public boolean insertTower(MapGisUtil.GeoPoint data) throws SQLException, IOException {
		boolean flag=true;
		if (data!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(INERT_TOWER);
				pst.setDouble(1, data.smX);
				pst.setDouble(2, data.smY);
				pst.setString(3, data.id);
				pst.setString(4, data.name);
				pst.setString(5, data.line_cim_id);
				pst.setString(6, data.linename);
				pst.setString(7, data.voltage);
				pst.setString(8, data.towerserialno);
				pst.setString(9, data.isshow);
				pst.setString(10, data.sel);
				pst.setString(11, data.towerno);
				pst.setString(12, data.is_loop);
				pst.setString(13, data.loopname);
				pst.setString(14, data.loop_cim_id);
				pst.execute();
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	public boolean insertTowers(List<MapGisUtil.GeoPoint> datas) throws SQLException, IOException {
		boolean flag=true;
		if (datas!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(INERT_TOWER);
				for (MapGisUtil.GeoPoint data : datas) {
					pst.setDouble(1, data.smX);
					pst.setDouble(2, data.smY);
					pst.setString(3, data.id);
					pst.setString(4, data.name);
					pst.setString(5, data.line_cim_id);
					pst.setString(6, data.linename);
					pst.setString(7, data.voltage);
					pst.setString(8, data.towerserialno);
					pst.setString(9, data.isshow);
					pst.setString(10, data.sel);
					pst.setString(11, data.towerno);
					pst.setString(12, data.is_loop);
					pst.setString(13, data.loopname);
					pst.setString(14, data.loop_cim_id);
					pst.addBatch();
				}
				pst.executeBatch();
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean editTower(MapGisUtil.GeoPoint data) throws SQLException, IOException {
		boolean flag=true;
		int result=0;
		if (data!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(UPDATE_TOWER);
				pst.setDouble(1, data.smX);
				pst.setDouble(2, data.smY);		
				pst.setString(3, data.name);
				pst.setString(4, data.line_cim_id);
				pst.setString(5, data.linename);
				pst.setString(6, data.voltage);
				pst.setString(7, data.towerserialno);
				pst.setString(8, data.isshow);
				pst.setString(9, data.sel);
				pst.setString(10, data.towerno);
				pst.setString(11, data.is_loop);
				pst.setString(12, data.loopname);
				pst.setString(13, data.loop_cim_id);
				pst.setString(14, data.id);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	
	/**
	 * delete from table name where condition1=？ and condition2=?
	 * @param tableName
	 * @param args [condition1,condition2]
	 * @return
	 * @throws Exception
	 */
	private boolean delete(String tableName,Object... args) throws Exception  {
		boolean flag=true;
		int result=0;
		if (args!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(tableName);
				for(int i = 0 ; i< args.length;i++){
					pst.setObject(i+1, args[i]);
				}
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
				throw e;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean delTower(String towerId) throws SQLException, IOException {
		boolean flag=true;
		int result=0;
		if (towerId!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(DELETE_TOWER);
				pst.setString(1, towerId);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	
	
	public boolean insertSubStation(MapGisUtil.GeoPoint2 data) throws SQLException, IOException {
		boolean flag=true;
		if (data!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(INERT_STATION);
				pst.setDouble(1, data.smX);
				pst.setDouble(2, data.smY);
				pst.setString(3, data.id);
				pst.setString(4, data.name);
				pst.setString(5, data.subControlArea);
				pst.setString(6, data.substationType);
				pst.setString(7, data.altitude);
				pst.setString(8, data.priority);
				pst.setString(9, data.pasId);
				pst.setString(10, data.voltageLevel);
				pst.setString(11, data.voltage);
				pst.setString(12, data.flag);
				pst.execute();
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean editSubStation(MapGisUtil.GeoPoint2 data) throws SQLException, IOException {
		boolean flag=true;
		int result=0;
		if (data!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(UPDATE_STATION);
				pst.setDouble(1, data.smX);
				pst.setDouble(2, data.smY);
				pst.setString(3, data.name);
				pst.setString(4, data.subControlArea);
				pst.setString(5, data.substationType);
				pst.setString(6, data.altitude);
				pst.setString(7, data.priority);
				pst.setString(8, data.pasId);
				pst.setString(9, data.voltageLevel);
				pst.setString(10, data.voltage);
				pst.setString(11, data.flag);
				pst.setString(12, data.id);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}

	public boolean delSubStation(String stationId) throws SQLException, IOException {
		boolean flag=true;
		int result=0;
		if (stationId!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(DELETE_STATION);
				pst.setString(1, stationId);
				result=pst.executeUpdate();
				if(result>0)
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	/**
	 * 检索是否已存在某条线路，choose为 single loop dc notower 线路标识参数
	 * 0 single； 1 loop； 2 dc； 3 notower；
	 * @param lineId
	 * @param choose
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public boolean searchLine(String lineId,int choose)throws SQLException, IOException {
		boolean flag=true;
		ResultSet result=null;
		if (lineId!=null) {
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				
				conn = server_datasource.getConnection();
				conn.setAutoCommit(false);
				if(choose==0){
					pst = conn.prepareStatement(SEARCH_SINGLE_LINE);
				}else if(choose==1){
					pst = conn.prepareStatement(SEARCH_LOOP_LINE);
				}else if(choose==2){
					pst = conn.prepareStatement(SEARCH_DC_LINE);
				}else if(choose==3){
					pst = conn.prepareStatement(SEARCH_NOTOWER_LINE);
				}
				pst.setString(1, lineId);
				result=pst.executeQuery();
				if(result.next())
					flag=true;
				else
					flag=false;
				conn.commit();
			} catch(Exception e){
				try {
					e.printStackTrace();
					conn.rollback();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				flag=false;
			}finally {
				result.close();
				pst.close();
				conn.close();
			}
		}
		return flag;
	}
	
}
